added some development tools
[windows-sources.git] / developer / Samples / NET 4.6 / Samples for Parallel / ParallelExtensionsExtras / Extensions / APM / FileAsync.cs
blob5fb9513b106b173685e481d5113547ea46728c3c
1 //--------------------------------------------------------------------------
2 //
3 // Copyright (c) Microsoft Corporation. All rights reserved.
4 //
5 // File: FileAsync.cs
6 //
7 //--------------------------------------------------------------------------
9 using System.Diagnostics;
10 using System.Text;
11 using System.Threading.Tasks;
13 namespace System.IO
15 /// <summary>Provides asynchronous counterparts to members of the File class.</summary>
16 public static class FileAsync
18 private const int BUFFER_SIZE = 0x2000;
20 /// <summary>Opens an existing file for asynchronous reading.</summary>
21 /// <param name="path">The path to the file to be opened for reading.</param>
22 /// <returns>A read-only FileStream on the specified path.</returns>
23 public static FileStream OpenRead(string path)
25 // Open a file stream for reading and that supports asynchronous I/O
26 return new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read, BUFFER_SIZE, true);
29 /// <summary>Opens an existing file for asynchronous writing.</summary>
30 /// <param name="path">The path to the file to be opened for writing.</param>
31 /// <returns>An unshared FileStream on the specified path with access for writing.</returns>
32 public static FileStream OpenWrite(string path)
34 // Open a file stream for writing and that supports asynchronous I/O
35 return new FileStream(path, FileMode.OpenOrCreate, FileAccess.Write, FileShare.None, BUFFER_SIZE, true);
38 /// <summary>
39 /// Opens a binary file for asynchronosu operation, reads the contents of the file into a byte array, and then closes the file.
40 /// </summary>
41 /// <param name="path">The path to the file to be read.</param>
42 /// <returns>A task that will contain the contents of the file.</returns>
43 public static Task<byte[]> ReadAllBytes(string path)
45 // Open the file for reading
46 var fs = OpenRead(path);
48 // Read all of its contents
49 var asyncRead = fs.ReadAllBytesAsync();
51 // When we're done reading its contents, close the file and propagate the file's contents
52 var closedFile = asyncRead.ContinueWith(t =>
54 fs.Close();
55 return t.Result;
56 }, TaskContinuationOptions.ExecuteSynchronously);
58 // Return the task that represents the entire operation being complete and that returns the
59 // file's contents
60 return closedFile;
63 /// <summary>
64 /// Opens a binary file for asynchronous operation, writes the contents of the byte array into the file, and then closes the file.
65 /// </summary>
66 /// <param name="path">The path to the file to be written.</param>
67 /// <returns>A task that will signal the completion of the operation.</returns>
68 public static Task WriteAllBytes(string path, byte[] bytes)
70 // Open the file for writing
71 var fs = OpenWrite(path);
73 // Write the contents to the file
74 var asyncWrite = fs.WriteAsync(bytes, 0, bytes.Length);
76 // When complete, close the file and propagate any exceptions
77 var closedFile = asyncWrite.ContinueWith(t =>
79 var e = t.Exception;
80 fs.Close();
81 if (e != null) throw e;
82 }, TaskContinuationOptions.ExecuteSynchronously);
84 // Return a task that represents the operation having completed
85 return closedFile;
88 /// <summary>
89 /// Opens a text file for asynchronosu operation, reads the contents of the file into a string, and then closes the file.
90 /// </summary>
91 /// <param name="path">The path to the file to be read.</param>
92 /// <returns>A task that will contain the contents of the file.</returns>
93 public static Task<string> ReadAllText(string path)
95 // Create a StringBuilder to store the text from the file and an encoding object to decode the
96 // contents of the file
97 var text = new StringBuilder();
98 var encoding = new UTF8Encoding();
100 // Open the file for reading
101 var fs = OpenRead(path);
103 // Continually read buffers from the file, decoding them and storing the results into the StringBuilder
104 var asyncRead = fs.ReadBuffersAsync(BUFFER_SIZE, (buffer, count) => text.Append(encoding.GetString(buffer, 0, count)));
106 // When done, close the file, propagate any exceptions, and return the decoded text
107 return asyncRead.ContinueWith(t =>
109 var e = t.Exception;
110 fs.Close();
111 if (e != null) throw e;
112 return text.ToString();
113 }, TaskContinuationOptions.ExecuteSynchronously);
116 /// <summary>
117 /// Opens a text file for asynchronosu operation, writes a string into the file, and then closes the file.
118 /// </summary>
119 /// <param name="path">The path to the file to be written.</param>
120 /// <returns>A task that will signal the completion of the operation.</returns>
121 public static Task WriteAllText(string path, string contents)
123 // First encode the string contents into a byte array
124 var encoded = Task.Factory.StartNew(
125 state => Encoding.UTF8.GetBytes((string)state),
126 contents);
128 // When encoding is done, write all of the contents to the file. Return
129 // a task that represents the completion of that write.
130 return encoded.ContinueWith(t => WriteAllBytes(path, t.Result)).Unwrap();